﻿@model IEnumerable<MVC_No06.Models.Task>

@{
    ViewBag.Title = "Index";
}

<style type="text/css">
</style>
<h2>Index</h2>

<p>
    @Html.ActionLink("添加", "Create")
</p>
@using (Html.BeginForm("Index", "MyHome", FormMethod.Get))
{
    <div style="padding-bottom:20px;width:100%" id="app">
        @Html.DropDownList("selectListItemsByUid", null, new { @class = "form-control", @id = "DropDownValueOfUid",@style="float:left;" })
        @Html.TextBox("Content", null, new { @class = "form-control", @style = "float:left;margin-left:15px;" })
        <input type="submit" value="提交" class="btn btn-info"style = "margin-left:15px;"  />
    </div>
}

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.TaskPriority)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Content)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AddTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TaskState)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FinishedTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.uid)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {


        <tr class="task_@item.TaskID">
            <td>
                @{
                    string taskPriority = "";
                    switch (item.TaskPriority)
                    {
                        case 1:
                            taskPriority = "高";
                            break;
                        case 2:
                            taskPriority = "中";
                            break;
                        case 3:
                            taskPriority = "低";
                            break;
                    }
                }
                @taskPriority
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Content)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AddTime)
            </td>
            <td>
                @{
                    string taskState = item.TaskState == 1 ? "未完成" : "已完成";
                }
                @taskState
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FinishedTime)
            </td>
            <td>
                @{
                    var dict = ViewData["userInfo"] as Dictionary<int, string>;
                    var uid = dict.FirstOrDefault(u => u.Key == item.uid).Value;
                }
                @uid
            </td>
            <td>
                <a onclick="UpdateBut(@item.TaskID)">修改</a>|
                <a onclick="DeleteBut(@item.TaskID)">删除</a>
            </td>
        </tr>
    }

</table>
@section scripts{
    <script src="~/Scripts/jquery-3.4.1.js"></script>
    <script src="~/Scripts/vue.js"></script>
    <script>
        $(function () {
            function UpdateBut(uid) {
                location = "/MyHome/Update?uid=" + uid;
            }

            function DeleteBut(uid) {
                $.post("/MyHome/Delete", { uid: uid }, function (data) {
                    switch (data) {
                        case 1:
                            console.log("删除成功");
                            $(".task_" + uid).remove();
                            break;
                        case 0:
                            console.log("删除失败");
                            break;
                        default:
                            console.log("未知");
                            break;
                    }
                })
            }

            $("#SelectBut").on("click", "#SelectBut", function () {
                console.log("emm");
                $.post("/MyHome/IndexOfDropDownList"), {}, function (data) {
                    /*ud.$data.uidArr = data;*/
                    console.log(data);
                }
            })

            var ud = new Vue({
                el: "#app",
                data: {
                    uidArr: [],
                    content: "",
                }
            });

            ($.post("/MyHome/IndexOfDropDownList"), {}, function (data) {
                /*ud.$data.uidArr = data;*/
                console.log(data);
            })()
        })
    </script>
}
